package com.example.demo.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import com.example.demo.APIS.DApi;
import javax.imageio.ImageIO;
/**
 * Created by Tity-PC
 * USER : 谢广泉
 * DATE : 2018/1/10.
 * TIME : 18:28
 */
public class DimensionalUitl {

    /**
     * 生成二维码(QRCode)图片
     * @param qrCodePath  二维码地址
     * @param iconPath  小图标
     * @param outPath 生成后图片
     */
    public static byte[] createQRCode(Image qrcode,Image icon) {
        try {


            //  二维码 地址

            //  小图标

            int w = qrcode.getWidth(null) ;
            int h = qrcode.getHeight(null) ;
            int iw = w / 4 ;
            int ih = h / 4 ;
            int icpw = (w/2)-(iw/2);
            int icph = (h/2)-(ih/2);

            BufferedImage bufImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics2D gs = bufImg.createGraphics();

            //擦除矩形块
            gs.clearRect(0, 0, w, h);

            //设置头像在二维码中的位置，建议将图片的大小尺寸设置在特定的值
            gs.drawImage(qrcode, 0, 0, null);
            // 缩小到指定大小
            icon = icon.getScaledInstance(iw,ih,Image.SCALE_DEFAULT);
            gs.drawImage(icon,icpw,icph,null );

            gs.dispose();
            bufImg.flush();

            // 处理后的图片
            /*
            File imgFile = new File(outPath);
            ImageIO.write(bufImg, "png", imgFile);
            */

           return DimensionalUitl.imageToBytes(bufImg,"png");

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 生成二维码(QRCode)图片
     * @param qrCodePath  二维码地址
     * @param iconPath  小图标
     * @param outPath 生成后图片
     */
    public static byte[] createQRCodeURL(URL qrCodePathUrl,URL iconUrl) {
        try {

            //  二维码 地址
            Image qrcode = ImageIO.read(qrCodePathUrl);

            //  小图标
            Image icon = ImageIO.read(iconUrl);

            return DimensionalUitl.createQRCode(qrcode,icon) ;


        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 转换BufferedImage 数据为byte数组
     *
     * @param image
     * Image对象
     * @param format
     * image格式字符串.如"gif","png"
     * @return byte数组
     */
    public static byte[] imageToBytes(BufferedImage bImage, String format) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, format, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }


    /**
     *  直接读取 oss png
     * @param qrCodePathUr
     * @return
     */
    public static byte[] createQRCodeURL(URL qrCodePathUr) {


        // 1 . 加载远程图片
        // 2.  绘制图片
        // 3.  输出图片流

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {

            Image qrcode = ImageIO.read(qrCodePathUr);

            BufferedImage bufImg = new BufferedImage(qrcode.getWidth(null), qrcode.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D gs = bufImg.createGraphics();

            gs.drawImage(qrcode, 0, 0, null);

            gs.dispose();
            bufImg.flush();

            ImageIO.write(bufImg, "png", out);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }


    public static void main(String[] args) {


        String qrCodePath = "C:\\Users\\Tity-PC\\Desktop\\img\\ae22fd529658b18e1ba3f690edf785a1.png";
        String iconPath = "C:\\Users\\Tity-PC\\Desktop\\img\\test.png";
        String outPath="C:\\Users\\Tity-PC\\Desktop\\img\\111.png";


        try {
            URL qrCodePathUrl = new URL("https://open.weixin.qq.com/connect/qrcode/081QWzVWn6O-rwt6");
            URL iconUrl = new URL("http://img.ui.cn/data/file/6/3/3/250336.png?imageMogr2/auto-orient/format/jpg/strip/thumbnail/!1800%3E/quality/90/");

            createQRCodeURL(qrCodePathUrl, iconUrl);
            System.out.println("encode success");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
